package be.gnx.fogo.application.ui;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import be.gnx.fogo.application.model.Person;

public abstract class AbstractParentGroup extends AbstractGroup {

  protected Person person;

  /*
   * Actually, the symbol and several others already exist and seem to be
   * standardized. The "Duden", the authoritative source on German orthography,
   * describes them in its section on typesetting practices, under the heading
   * of "genealogical symbols". Besides the asterisk (born), the dagger/cross
   * (died), and the two overlapping rings (married) it lists: wavy horizontal
   * line (= baptized) two rings joined by a horizontal line (= extramarital)
   * two swords crossed (= died in combat) rectangle (= buried) urn symbol (=
   * cremated)
   */
  private static char bornSymbol = '\u002A'; // or '\u2605'

  private static char diedSymbol = '\u2020';

  private static char marriedSymbol = '\u26AD'; // or '\u221E'

  private static char divorcedSymbol = '\u26AE'; // or '\u29DE'

  private static char engagedSymbol = '\u26AC';

  private Text nameText;

  private Text birthText;

  private Text deathText;

  private Image personImage;

  protected AbstractParentGroup(Composite composite, int style, ResourceBundle resourceBundle) {
    super(composite, style, resourceBundle);
    getGroup().setLayout(new RowLayout());
    nameText = new Text(getGroup(), SWT.NONE);
    birthText = new Text(getGroup(), SWT.NONE);
    deathText = new Text(getGroup(), SWT.NONE);
  }

  protected void setPerson(Person person) {
    this.person = person;
    if (person != null) {
      getGroup().setToolTipText(person.getAlternativeName().getValue() + " " + person.getSurname().getValue());
      StringBuffer stringBuffer = new StringBuffer();
      stringBuffer.append(person.getAlternativeName().getValue());
      stringBuffer.append(' ');
      stringBuffer.append(person.getSurname().getValue());
      nameText.setText(stringBuffer.toString());

      StringBuffer birth = new StringBuffer();
      if ((person.getDateOfBirthDay() != null) || (person.getDateOfBirthMonth() != null)
          || (person.getDateOfBirthYear() != null)) {
        birth.append(' ');
        birth.append(bornSymbol);
      }
      if ((person.getDateOfBirthDay() != null) && (person.getDateOfBirthMonth() != null)) {
        birth.append(person.getDateOfBirthDay());
        birth.append('/');
      }
      if (person.getDateOfBirthMonth() != null) {
        birth.append(person.getDateOfBirthMonth());
      }
      if ((person.getDateOfBirthMonth() != null) && (person.getDateOfBirthYear() != null)) {
        birth.append('/');
      }
      if (person.getDateOfBirthYear() != null) {
        birth.append(person.getDateOfBirthYear());
      }
      birthText.setText(birth.toString());

      StringBuffer death = new StringBuffer();
      if ((person.getDateOfDeathDay() != null) || (person.getDateOfDeathMonth() != null)
          || (person.getDateOfDeathYear() != null)) {
        death.append(' ');
        death.append(diedSymbol);
      }
      if ((person.getDateOfDeathDay() != null) && (person.getDateOfDeathMonth() != null)) {
        death.append(person.getDateOfDeathDay());
        death.append('/');
      }
      if (person.getDateOfDeathMonth() != null) {
        death.append(person.getDateOfDeathMonth());
      }
      if ((person.getDateOfDeathMonth() != null) && (person.getDateOfDeathYear() != null)) {
        death.append('/');
      }
      if (person.getDateOfDeathYear() != null) {
        death.append(person.getDateOfDeathYear());
      }
      deathText.setText(death.toString());

      // getGroup().pack(true);
    } else {
      // TODO
    }
  }

  @Override
  protected void resize() {
    super.resize();
    nameText.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
    birthText.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
    deathText.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
  }

}
